angular.module('CheckConfirm')
    .controller('CheckConfirm.ResultCtrl', ['$state', '$scope', '$ionicHistory', 'User', 'Http', 'imanDialog', 'CheckConfirm.CheckService', 'Device',
        function ($state, $scope, $ionicHistory, User, Http, imanDialog, checkService, Device) {
            $scope.data = {
                container: checkService.getContainer(),
                department: Device.get().Department,
                scanQualifiedArray: checkService.getScanQualifiedObj(),
                scanUnQualifiedArray: checkService.getScanUnQualifiedArray(),
                ValidBarcodeList: checkService.getValidBarcodeList(),
                inValidBarcodeList: checkService.getInValidBarcodeList()
            };

            $scope.confirm = function () {
                if (!$scope.data.container || !$scope.data.container._id) {
                    imanDialog.toastBottom('货架信息为空');
                    return;
                }

                if ($scope.data.ValidBarcodeList.length === 0) {
                    imanDialog.toastBottom('球拍合格数量为0,无法提交');
                    return;
                }
                
                imanDialog.showLoading();
                Http.post({
                    url: '/api/Kanban/Arrived',
                    data: {
                        Parameter: {
                            userId: User.get()._id,
                            containerId: $scope.data.container._id,
                            validBarcodeList: $scope.data.ValidBarcodeList,
                            inValidBarcodeList: $scope.data.inValidBarcodeList,
                            departmentId: Device.get().Department._id,
                            TerminalId: Device.get().Terminal._id,
                            TerminalName: Device.get().Terminal.Name,
                            WorkId: Device.get().Work._id,
                            WorkName: Device.get().Work.Name,
                            wareHouse: checkService.getWareHouse(),
                            WorkLocationId: Device.get().WorkLocation._id,
                            WorkLocationName: Device.get().WorkLocation.Name
                        }
                    }
                }).then(function () {
                    imanDialog.toastCenter('提交成功');
                    checkService.setContainer();
                    $ionicHistory.goBack(-2);
                }).finally(function () {
                    imanDialog.hideLoading();
                });
            };
        }
    ]);
